package edu.fib.upc.mapfile_writer4and;

import org.openstreetmap.osmosis.core.Osmosis;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);        

        String[] args;
                
        Bundle extras = getIntent().getExtras();
        //check to see if "myKey" is in the bundle, if so then assign it's value
        // to mIntentString  if not, assign "nothing passed in" to mIntentString...
        args = extras != null ? extras.getStringArray("args") : null;        
        
        if(args != null) {
        	// if arguments have been passed        	   	        	        	
        	new OsmosisCallTask().execute(args);
        }        
        else {
        	// start user interface
        	setContentView(R.layout.input_layout);        	
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);                
        return true;
    }
    
    public void parseInputAndRun(View v) {
    	// Retrieve the args textEdit text.
    	String [] args = null;
    	
    	EditText input = (EditText) findViewById(R.id.editText1);
    	String command = input.getText().toString();
    	// Split the string and form an ARGS String[]
    	
    	args = command.split(" ");
    	
    	// Call Osmosis
    	new OsmosisCallTask().execute(args);
    }
    
	private class OsmosisCallTask extends AsyncTask<String, Integer, String> {	

		@Override
		protected String doInBackground(String...args) {
        	Osmosis.run(args);        	
        	return "";  
		}

		@Override
		protected void onPreExecute() {		
			// Display progress bar and such
        	setContentView(R.layout.activity_main);     
        	
			TextView tv = (TextView) findViewById(R.id.textView1);        	
        	tv.setText(R.string.please_wait);
        	
        	ProgressBar pb = (ProgressBar) findViewById(R.id.progressBar1);
        	pb.setIndeterminate(true);
        	pb.setProgress(0);
        	pb.invalidate();
        	
        	return;
		}

		@Override
		protected void onProgressUpdate(Integer... ints) {
			// TODO: Notify the user that the process is being done more accurately
			// Update the progress bar
			return;
		}

		@Override
		protected void onPostExecute(String message) {
			Toast.makeText(getApplicationContext(), "Process ended. " + message, Toast.LENGTH_LONG).show();
			
			TextView tv = (TextView) findViewById(R.id.textView1);        	
        	tv.setText(R.string.finished);
        	
        	ProgressBar pb = (ProgressBar) findViewById(R.id.progressBar1);
        	pb.setIndeterminate(false);
        	pb.setProgress(pb.getMax());
        	pb.invalidate();
        	
        	finish();
			return;
		}
	}
}
